/*******************************************************************************
 * Chatty - Mobile Messaging - J2ME ICQ clone
 * Copyright (C) 2003-05  Chatty Project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *******************************************************************************
 * File: src/chatty/Options.java
 * Version: ###VERSION###  Date: ###DATE###
 * Author(s): Manuel Linsmayer, Andreas Rossbacher, Artyomov Denis, Igor Palkin,
 * Vladimir Kryukov
 ******************************************************************************/
package chatty;

import java.util.*;
import javax.microedition.lcdui.*;

import chatty.cl.ContactList;
import chatty.comm.*;
import chatty.modules.*;
import chatty.ui.*;
import chatty.ui.base.*;
import chatty.ui.form.*;
import chatty.ui.menu.*;
import chatty.util.*;

import protocol.*;
import protocol.simp.*;

/* Form for editing option values */
public class OptionsForm implements FormListener, SelectListener {

    private MenuModel optionsMenu = new MenuModel(MenuModel.MENU_CENTER);
    private MenuModel colorScheme = null;
	private MenuModel skinMenu = null;
    private MenuModel hotkey = null;
    private MenuModel hotkeyAction = null;
    private GraphForm form = new GraphForm("options_lng", "save", "back", this);
    private int currentOptionsForm;

    // Static constants for menu actios
    private static final int OPTIONS_ACCOUNT    = 0;
    private static final int OPTIONS_NETWORK    = 1;
    private static final int OPTIONS_INTERFACE  = 3;
    private static final int OPTIONS_SCHEME     = 4;
	private static final int OPTIONS_SKIN      = 5;
    private static final int OPTIONS_HOTKEYS    = 6;
    private static final int OPTIONS_SIGNALING  = 7;
    // #sijapp cond.if modules_TRAFFIC is "true"#
    private static final int OPTIONS_TRAFFIC    = 8;
    // #sijapp cond.end#
    
    private static final int OPTIONS_TIMEZONE   = 9;
    private static final int OPTIONS_ANTISPAM   = 10;
    private static final int OPTIONS_LIGHT      = 11;
    private static final int OPTIONS_ABSENCE    = 12;
    private static final int OPTIONS_ANSWERER   = 13;
	
    // Exit has to be biggest element cause it also marks the size
    private static final int MENU_EXIT          = 14;
    
    final private String[] hotkeyActionNames = Util.explode(
            "ext_hotkey_action_none"
            + "|" + "info"
            + "|" + "open_chats"
            // #sijapp cond.if modules_HISTORY is "true"#
            + "|" + "history"
            // #sijapp cond.end#
            + "|" + "ext_hotkey_action_onoff"
            + "|" + "keylock"
            // #sijapp cond.if target is "MIDP2"#
            + "|" + "minimize"
            // #sijapp cond.end#
            + "|" + "#sound_off"
            // #sijapp cond.if modules_MAGIC_EYE is "true" #
            + "|" + "magic eye"
            // #sijapp cond.end#
            // #sijapp cond.if modules_LIGHT is "true" #
            + "|" + "light_control"
            // #sijapp cond.end#
            + "|" + "user_statuses"
            + "|" + "collapse_all",
            '|'
            );
    
    final private int [] hotkeyActions = {
        Options.HOTKEY_NONE,
        Options.HOTKEY_INFO,
        Options.HOTKEY_OPEN_CHATS,
        // #sijapp cond.if modules_HISTORY is "true"#
        Options.HOTKEY_HISTORY,
        // #sijapp cond.end#
        Options.HOTKEY_ONOFF,
        Options.HOTKEY_LOCK,
        // #sijapp cond.if target is "MIDP2"#
        Options.HOTKEY_MINIMIZE,
        // #sijapp cond.end#
        Options.HOTKEY_SOUNDOFF,
        // #sijapp cond.if modules_MAGIC_EYE is "true" #
        Options.HOTKEY_MAGIC_EYE,
        // #sijapp cond.end#
        // #sijapp cond.if modules_LIGHT is "true" #
        Options.HOTKEY_LIGHT,
        // #sijapp cond.end#
        Options.HOTKEY_STATUSES,
        Options.HOTKEY_COLLAPSE_ALL
    };
    
    public OptionsForm() {
    }
    
    
    // Initialize the kist for the Options menu
    private void initOptionsList() {
        optionsMenu.clean();
        
        optionsMenu.addItem("options_account", Chatty.optionsIcons.iconAt(Chatty.ICON_ACCOUNT), OPTIONS_ACCOUNT);
        optionsMenu.addItem("options_network", Chatty.optionsIcons.iconAt(Chatty.ICON_NETWORK), OPTIONS_NETWORK);
        optionsMenu.addItem("options_interface", Chatty.optionsIcons.iconAt(Chatty.ICON_INTERFACE), OPTIONS_INTERFACE);
        
        if (Scheme.getSchemeNames().length > 2) {
            optionsMenu.addItem("color_scheme", Chatty.optionsIcons.iconAt(Chatty.ICON_SCHEME), OPTIONS_SCHEME);
        }
        if (Skin.getSkinNames().length > 2) {
            optionsMenu.addItem("skin", Chatty.optionsIcons.iconAt(Chatty.ICON_SKIN), OPTIONS_SKIN);
        }
        if (2 != Options.getInt(Options.OPTION_KEYBOARD)) {
            optionsMenu.addItem("options_hotkeys", Chatty.optionsIcons.iconAt(Chatty.ICON_HOTKEYS), OPTIONS_HOTKEYS);
        }
        optionsMenu.addItem("options_signaling", Chatty.optionsIcons.iconAt(Chatty.ICON_SIGNALING), OPTIONS_SIGNALING);
        // #sijapp cond.if modules_ANTISPAM is "true"#
        optionsMenu.addItem("antispam", Chatty.optionsIcons.iconAt(Chatty.ICON_ANTISPAM), OPTIONS_ANTISPAM);
        // #sijapp cond.end#
        // #sijapp cond.if modules_LIGHT is "true" #
        if (CustomLight.isSupport()) {
            optionsMenu.addItem("light_control", Chatty.optionsIcons.iconAt(Chatty.ICON_LIGHT), OPTIONS_LIGHT);
        }
        // #sijapp cond.end#
        // #sijapp cond.if modules_ABSENCE is "true" #
        optionsMenu.addItem("absence", Chatty.optionsIcons.iconAt(Chatty.ICON_ABSENCE), OPTIONS_ABSENCE);
        // #sijapp cond.end#
		
        // #sijapp cond.if modules_ANSWERER is "true" #
		optionsMenu.addItem("answerer", Chatty.optionsIcons.iconAt(Chatty.ICON_ANSWERER), OPTIONS_ANSWERER);
		// #sijapp cond.end#
		
		// #sijapp cond.if modules_TRAFFIC is "true"#
        optionsMenu.addItem("traffic_lng", Chatty.menuIcons.iconAt(Chatty.ICON_TRAFFIC), OPTIONS_TRAFFIC);
        // #sijapp cond.end#
        optionsMenu.addItem("time_zone", Chatty.optionsIcons.iconAt(Chatty.ICON_TIMEZONE), OPTIONS_TIMEZONE);
        optionsMenu.setActionListener(this);
        optionsMenu.setDefaultItemCode(currentOptionsForm);
    }
    
    private void addHotKey(String keyName, int option) {
        int optionValue = Options.getInt(option);
        String name = null;
        for (int i = 0; i < hotkeyActionNames.length; ++i) {
            if (hotkeyActions[i] == optionValue) {
                name = ResBundle.getString(keyName) + ": "
                        + ResBundle.getString(hotkeyActionNames[i]);
            }
        }
        if (null == name) {
            name = ResBundle.getString(keyName) + ": <???>";
        }
        hotkey.addRawItem(name, null, option);
    }
    
    private int currentHotkey = -1;
    private void initHotkeyMenuUI() {
        if (null == hotkey) {
            hotkey = new MenuModel(MenuModel.MENU_CENTER);
        }
        hotkey.clean();
        addHotKey("ext_clhotkey0",     Options.OPTION_EXT_CLKEY0);
        addHotKey("ext_clhotkey4",     Options.OPTION_EXT_CLKEY4);
        addHotKey("ext_clhotkey6",     Options.OPTION_EXT_CLKEY6);
        addHotKey("ext_clhotkeystar",  Options.OPTION_EXT_CLKEYSTAR);
        addHotKey("ext_clhotkeypound", Options.OPTION_EXT_CLKEYPOUND);
        // #sijapp cond.if target is "MIDP2" #
        if (Chatty.isPhone(Chatty.PHONE_SE)) {
            addHotKey("camera", Options.OPTION_EXT_CLKEYCALL);
        } else {
            addHotKey("ext_clhotkeycall", Options.OPTION_EXT_CLKEYCALL);
        }
        // #sijapp cond.end#
        hotkey.setDefaultItemCode(currentHotkey);
        hotkey.setActionListener(this);
    }
    
    private void initHotkeyActionMenuUI() {
        if (null == hotkeyAction) {
            hotkeyAction = new MenuModel(MenuModel.MENU_CENTER);
        }
        hotkeyAction.clean();
        for (int i=0; i < hotkeyActionNames.length; ++i) {
            hotkeyAction.addItem(hotkeyActionNames[i], hotkeyActions[i]);
        }
        hotkeyAction.setDefaultItemCode(Options.getInt(currentHotkey));
        hotkeyAction.setActionListener(this);
    }
    
    ///////////////////////////////////////////////////////////////////////////
    /* Activate options menu */
    public static void activate() {
        OptionsForm instance = new OptionsForm();
        instance.initOptionsList();
        new Select(instance.optionsMenu).show();
    }
    
    private void showThemeSelector() {
        String[] strings = Scheme.getSchemeNames();
        colorScheme = null;
        if (strings.length > 1) {
            colorScheme = new MenuModel(MenuModel.MENU_CENTER);
            colorScheme.setActionListener(this);
            for (int i = 0; i < strings.length; ++i) {
                colorScheme.addRawItem(strings[i], null, i);
            }
            colorScheme.setDefaultItemCode(Options.getInt(Options.OPTION_COLOR_SCHEME));
            new Select(colorScheme).show();
        }
    }
	
    private void showSkinSelector() {
        String[] strings = Skin.getSkinNames();
        skinMenu = null;
        if (strings.length > 1) {
            skinMenu = new MenuModel(MenuModel.MENU_CENTER);
            skinMenu.setActionListener(this);
            for (int i = 0; i < strings.length; ++i) {
                skinMenu.addRawItem(strings[i], null, i);
            }
            skinMenu.setDefaultItemCode(Options.getInt(Options.OPTION_SKIN));
            new Select(skinMenu).show();
        }
    }
	
    private void setChecked(String lngStr, int optValue) {
        form.addCheckBox(optValue, lngStr, Options.getBoolean(optValue));
    }
    
    
    private void createNotifyControls(int modeOpt, int volumeOpt, int notifyType, String title, boolean typping) {
        // #sijapp cond.if modules_SOUND is "true" #
        Notify notify = Notify.getSound();
        // #sijapp cond.end #
        String list = "no";
        if (typping) {
            list += "|" + "typing_display_only";
        }
        // #sijapp cond.if modules_SOUND is "true" #
        list += "|" + "beep";
        if (notify.hasSound(notifyType)) {
            list += "|" + "sound";
        }
        // #sijapp cond.end#
        createSelector(title, list, modeOpt);
        // #sijapp cond.if modules_SOUND is "true" #
        if (notify.hasSound(notifyType)) {
            loadOptionGauge(volumeOpt, "volume");
        }
        // #sijapp cond.end#
    }
    
    /* Helpers for options UI: */
    private void createSelector(String cap, String items, int opt) {
        form.addSelector(opt, cap, items, Options.getInt(opt));
    }
    private void loadOptionString(int opt, String label, int size, int type) {
        form.addTextField(opt, label, Options.getString(opt), size, type);
    }
    private void saveOptionString(int opt) {
        Options.setString(opt, form.getTextFieldValue(opt));
    }
    private void loadOptionInt(int opt, String label, int size) {
        form.addTextField(opt, label, String.valueOf(Options.getInt(opt)), size, TextField.NUMERIC);
    }
    private void saveOptionInt(int opt, int defval, int min, int max) {
        int val = Util.strToIntDef(form.getTextFieldValue(opt), defval);
        Options.setInt(opt, Math.max(min, Math.min(val, max)));
    }
    private void saveOptionBoolean(int opt) {
        Options.setBoolean(opt, form.getCheckBoxValue(opt));
    }
    private void saveOptionSelector(int opt) {
        Options.setInt(opt, form.getSelectorValue(opt));
    }
    // #sijapp cond.if modules_SOUND is "true" #
    // #sijapp cond.if target isnot "RIM"#
    private void loadOptionGauge(int opt, String label) {
        form.addVolumeControl(opt, label, Options.getInt(opt));
    }
    private void saveOptionGauge(int opt) {
        Options.setInt(opt, form.getVolumeValue(opt));
    }
    // #sijapp cond.end#
    // #sijapp cond.end#
    // #sijapp cond.if modules_TRAFFIC is "true"#
    private void loadOptionDecimal(int opt, String label) {
        form.addTextField(opt, label, Util.intToDecimal(Options.getInt(opt)),
                6, TextField.ANY);
    }
    private void saveOptionDecimal(int opt) {
        Options.setInt(opt, Util.decimalToInt(form.getTextFieldValue(opt)));
    }
    // #sijapp cond.end#
    // #sijapp cond.if modules_LIGHT is "true"#
    private void loadLightValue(String title, int id) {
        int value = Options.getInt(id);
        form.addTextField(id, title, String.valueOf(value), 3, TextField.DECIMAL);
    }
    private void saveLightValue(int opt, int min, int max, int def) {
        saveOptionInt(opt, def, min, max);
    }
    // #sijapp cond.end#
    
    private static final int autoConnectChoiceGroup = 1002;
    private static final int connPropChoiceGroup = 1003;
    private static final int choiceInterfaceMisc = 1004;
    private static final int choiceContactList = 1005;
    private static final int chrgChat = 1006;
    private static final int chrgStatus = 1007;
    private static final int chrgMessages = 1008;
    private static final int chrgIcons = 1009;
    private static final int chsNotifyAct = 1010;
    private static final int chrgAntispam = 1015;
    
    /* Command listener */
    public void formAction(GraphForm form, boolean apply) {
        /* Look for back command */
        if (!apply) {
            back();
            
            // Look for save command
        } else {
            // Save values, depending on selected option menu item
            switch (currentOptionsForm) {

                case OPTIONS_NETWORK:
                    saveOptionString(Options.OPTION_CONN_ALIVE_INVTERV);
                    saveOptionBoolean(Options.OPTION_AUTO_CONNECT);
                    saveOptionBoolean(Options.OPTION_RECONNECT);
                    saveOptionInt(Options.OPTION_RECONNECT_NUMBER, 1, 0, 50);
                    initOptionsList();
                    break;
					
                case OPTIONS_INTERFACE:
                    if (ResBundle.langAvailable.length > 1) {
                        int lang = form.getSelectorValue(Options.OPTION_UI_LANGUAGE);
                        Options.setString(Options.OPTION_UI_LANGUAGE, ResBundle.langAvailable[lang]);
                    }
                    
                    int idx = 0;
                    saveOptionBoolean(Options.OPTION_SWAP_SOFT_KEY);
                    saveOptionBoolean(Options.OPTION_CHECK_UPDATES);
                    
                    idx = 0;
					
					
					saveOptionBoolean(Options.OPTION_ACCOUNT_GROUPS);
                    saveOptionBoolean(Options.OPTION_USER_GROUPS);
					saveOptionBoolean(Options.OPTION_TOOLBAR);
					saveOptionBoolean(Options.OPTION_ANIMATION);
                    saveOptionBoolean(Options.OPTION_CL_HIDE_OFFLINE);
                    saveOptionBoolean(Options.OPTION_SAVE_TEMP_CONTACT);
                    saveOptionBoolean(Options.OPTION_SORT_UP_WITH_MSG);
                    saveOptionBoolean(Options.OPTION_SHOW_STATUS_LINE);
					saveOptionBoolean(Options.OPTION_SHOW_AVATARS);
					saveOptionBoolean(Options.OPTION_CENTERED_STYLE_MENU);
					//saveOptionBoolean(Options.OPTION_LOAD_MENU_ICONS);
                    // #sijapp cond.if modules_XSTATUSES is "true" #
                    saveOptionBoolean(Options.OPTION_REPLACE_STATUS_ICON);
                    // #sijapp cond.end#
                    saveOptionSelector(Options.OPTION_CL_SORT_BY);
					
                    
                    idx = 0;
					saveOptionBoolean(Options.OPTION_ANSWERER);
					saveOptionBoolean(Options.OPTION_MINICHAT);
                    // #sijapp cond.if modules_HISTORY is "true"#
                    saveOptionBoolean(Options.OPTION_HISTORY);
                    saveOptionBoolean(Options.OPTION_SHOW_LAST_MESS);
                    // #sijapp cond.end#
                    // #sijapp cond.if target is "MIDP2" #
                    saveOptionBoolean(Options.OPTION_CLASSIC_CHAT);
                    // #sijapp cond.end#
                    // #sijapp cond.if target is "MIDP2" | target is "MOTOROLA" #
                    saveOptionBoolean(Options.OPTION_SWAP_SEND_AND_BACK);
                    // #sijapp cond.end#
                    // #sijapp cond.if target is "MIDP2" | target is "MOTOROLA" #
                    saveOptionBoolean(Options.OPTION_TF_FLAGS);
                    // #sijapp cond.end#
                    saveOptionBoolean(Options.OPTION_UNTITLED_INPUT);
                    
                    idx = 0;
                    saveOptionBoolean(Options.OPTION_DELIVERY_NOTIFICATION);
                                        
                    // #sijapp cond.if modules_XSTATUSES is "true" #
                    saveOptionBoolean(Options.OPTION_AUTO_XTRAZ);
                    // #sijapp cond.end #
                    
                    saveOptionSelector(Options.OPTION_FONT_SCHEME);
                    GraphicsEx.setFontScheme(Options.getInt(Options.OPTION_FONT_SCHEME));

                    saveOptionInt(Options.OPTION_MAX_MSG_COUNT, 100, 20, 500);
                    saveOptionSelector(Options.OPTION_MSGSEND_MODE);
                    saveOptionSelector(Options.OPTION_INPUT_MODE);
                    saveOptionSelector(Options.OPTION_KEYBOARD);
                    
                    final String sysLang = ResBundle.getCurrUiLanguage();
                    final String newLang = Options.getString(Options.OPTION_UI_LANGUAGE);
                    if (!sysLang.equals(newLang)) {
                        Options.setBoolean(Options.OPTIONS_LANG_CHANGED, true);
                    }
                    ContactList.getInstance().getManager().update();
                    break;
                    
                case OPTIONS_SIGNALING:
                    saveOptionSelector(Options.OPTION_VIBRATOR);
                    // #sijapp cond.if modules_SOUND is "true" #
                    saveOptionSelector(Options.OPTION_ONLINE_NOTIF_MODE);
                    saveOptionGauge(Options.OPTION_ONLINE_NOTIF_VOL);
                    saveOptionSelector(Options.OPTION_MESS_NOTIF_MODE);
                    saveOptionGauge(Options.OPTION_MESS_NOTIF_VOL);
                    // #sijapp cond.end#
                    saveOptionSelector(Options.OPTION_TYPING_MODE);

                    saveOptionSelector(Options.OPTION_POPUP_WIN2);
                    idx = 0;
                    saveOptionBoolean(Options.OPTION_ALARM);
                    saveOptionBoolean(Options.OPTION_POPUP_OVER_SYSTEM);
                    // #sijapp cond.if target="MIDP2"#
                    saveOptionBoolean(Options.OPTION_BRING_UP);
                    // #sijapp cond.end#
                    saveOptionBoolean(Options.OPTION_CUSTOM_GC);
                    break;
                    
                    // #sijapp cond.if modules_ANTISPAM is "true"#
                case OPTIONS_ANTISPAM:
                    saveOptionString(Options.OPTION_ANTISPAM_MSG);
                    saveOptionString(Options.OPTION_ANTISPAM_ANSWER);
                    saveOptionString(Options.OPTION_ANTISPAM_HELLO);
                    saveOptionString(Options.OPTION_ANTISPAM_KEYWORDS);
                    saveOptionBoolean(Options.OPTION_ANTISPAM_ENABLE);
                    saveOptionBoolean(Options.OPTION_ANTISPAM_OFFLINE);
                    break;
                    // #sijapp cond.end#
                    
                    // #sijapp cond.if modules_LIGHT is "true"#
                case OPTIONS_LIGHT:
                    saveLightValue(Options.OPTION_LIGHT_NONE, -1, 101, 0);
                    saveLightValue(Options.OPTION_LIGHT_KEY_PRESS, -1, 101, 100);
                    saveLightValue(Options.OPTION_LIGHT_MESSAGE, -1, 101, 100);
                    saveLightValue(Options.OPTION_LIGHT_ONLINE, -1, 101, 101);
                    saveLightValue(Options.OPTION_LIGHT_SYSTEM, 1, 100, 100);
                    saveLightValue(Options.OPTION_LIGHT_TICK, 1, 999, 15);
                    saveOptionBoolean(Options.OPTION_LIGHT);
                    CustomLight.switchOn(Options.getBoolean(Options.OPTION_LIGHT));
                    break;
                    // #sijapp cond.end#
                    
                    // #sijapp cond.if modules_ABSENCE is "true" #
                case OPTIONS_ABSENCE:
                    saveOptionBoolean(Options.OPTION_AA_BLOCK);
                    Options.setInt(Options.OPTION_AA_TIME, form.getSelectorValue(Options.OPTION_AA_TIME) * 5);
                    chatty.modules.AutoAbsence.instance.updateOptions();
                    break;
                    // #sijapp cond.end#
					
                    // #sijapp cond.if modules_TRAFFIC is "true"#
                case OPTIONS_TRAFFIC:
                    saveOptionDecimal(Options.OPTION_COST_OF_1M);
                    saveOptionDecimal(Options.OPTION_COST_PER_DAY);
                    Options.setInt(Options.OPTION_COST_PACKET_LENGTH,
                            Util.strToIntDef(form.getTextFieldValue(Options.OPTION_COST_PACKET_LENGTH), 0) * 1024);
                    saveOptionString(Options.OPTION_CURRENCY);
                    break;
                    // #sijapp cond.end#
                    
                case OPTIONS_TIMEZONE: {
                    /* Set up time zone*/
                    int timeZone = form.getSelectorValue(Options.OPTIONS_GMT_OFFSET) - 12;
                    Options.setInt(Options.OPTIONS_GMT_OFFSET, timeZone);
                    
                    /* Translate selected time to GMT */
                    int[] currDateTime = Util.createDate(Util.createCurrentDate(false));
                    int hour = currDateTime[Util.TIME_HOUR];
                    int selHour = form.getSelectorValue(Options.OPTIONS_LOCAL_OFFSET) - timeZone;
                    selHour = selHour - 12;
                    
                    /* Calculate diff. between selected GMT time and phone time */
                    int localOffset = (selHour + 12 + 24) % 24 - 12;
                    Options.setInt(Options.OPTIONS_LOCAL_OFFSET, localOffset);
                    break;
                }
                
            }
            
            /* Save options */
            Options.safeSave();
            back();
        }
        
    }
    
    public void select(Select select, MenuModel model, int cmd) {
        if (hotkey == model) {
            currentHotkey = cmd;
            initHotkeyActionMenuUI();
            new Select(hotkeyAction).show();
            return;
        }
        if (hotkeyAction == model) {
            final int hotkeyAction = cmd;
            Options.setInt(currentHotkey, hotkeyAction);
            Options.safeSave();
            initHotkeyMenuUI();
            select.back();
            return;
        }
        
        if (colorScheme == model) {
            final int currentColorScheme = cmd;
            Scheme.setColorScheme(currentColorScheme);
            Options.safeSave();
            select.back();
            colorScheme = null;
            return;
        }
        if (skinMenu == model) {
            Skin.setSkin(cmd);
            Options.safeSave();
            select.back();
            skinMenu = null;
            return;
        }		
        // Add elements, depending on selected option menu item
        currentOptionsForm = cmd;
        // Delete all items
        form.clearForm();
        form.setCaption(model.getItemText(currentOptionsForm));
        switch (currentOptionsForm) {
            case OPTIONS_ACCOUNT:
                new AccountsForm().show();
                return;
                
            case OPTIONS_NETWORK:
                loadOptionString(Options.OPTION_CONN_ALIVE_INVTERV, "timeout_interv", 3, TextField.NUMERIC);
                
                form.addString("conn_prop", null);
                setChecked("reconnect", Options.OPTION_RECONNECT);
                
                setChecked("auto_connect", Options.OPTION_AUTO_CONNECT);
                
                loadOptionInt(Options.OPTION_RECONNECT_NUMBER, "reconnect_number", 2);
                break;

            case OPTIONS_INTERFACE:
                // Initialize elements (interface section)
                if (ResBundle.langAvailable.length > 1) {
                    String items = "";
                    int cur = 0;
                    for (int j = 0; j < ResBundle.langAvailable.length; ++j) {
                        boolean selected = ResBundle.langAvailable[j].equals(Options.getString(Options.OPTION_UI_LANGUAGE));
                        items += "|" + ResBundle.langAvailableName[j];
                        if (selected) {
                            cur = j;
                        }
                    }
                    items = items.substring(1);
                    form.addSelector(Options.OPTION_UI_LANGUAGE, "language", items, cur);
                }
                
                form.addString("misc", null);
                setChecked("swap_soft_key", Options.OPTION_SWAP_SOFT_KEY);
                setChecked("check_updates", Options.OPTION_CHECK_UPDATES);
                
                createSelector("sort_by",
                        "sort_by_status" + "|" + "sort_by_online" + "|" + "sort_by_name",
                        Options.OPTION_CL_SORT_BY);
                
                form.addString("contact_list", null);
				setChecked("show_account_groups",		Options.OPTION_ACCOUNT_GROUPS);
                setChecked("show_user_groups",			Options.OPTION_USER_GROUPS);
				
				if (NativeCanvas.getInstance().hasPointerEvents() && chatty.Chatty.icons != null)
					setChecked("toolbar",					Options.OPTION_TOOLBAR);
					
				setChecked("animation",			     	Options.OPTION_ANIMATION);
				
                setChecked("hide_offline",				Options.OPTION_CL_HIDE_OFFLINE);
                setChecked("save_temp_contacts",		Options.OPTION_SAVE_TEMP_CONTACT);
                setChecked("contacts_with_msg_at_top",	Options.OPTION_SORT_UP_WITH_MSG);
                setChecked("show_status_line",			Options.OPTION_SHOW_STATUS_LINE);
				setChecked("show_avatars",				Options.OPTION_SHOW_AVATARS);
				setChecked("centered_style_menu",		Options.OPTION_CENTERED_STYLE_MENU);
				//setChecked("load_menu_icons",			Options.OPTION_LOAD_MENU_ICONS);
                // #sijapp cond.if modules_XSTATUSES is "true" #
                setChecked("replace_status_icon",		Options.OPTION_REPLACE_STATUS_ICON);
                // #sijapp cond.end#
                
                form.addString("chat", null);
				setChecked("minichat",     Options.OPTION_MINICHAT);
                // #sijapp cond.if modules_HISTORY is "true"#
                setChecked("use_history",     Options.OPTION_HISTORY);
                setChecked("show_prev_mess",  Options.OPTION_SHOW_LAST_MESS);
                // #sijapp cond.end#
                // #sijapp cond.if target is "MIDP2" #
                setChecked("cl_chat",         Options.OPTION_CLASSIC_CHAT);
                // #sijapp cond.end#
                // #sijapp cond.if target is "MIDP2" | target is "MOTOROLA" #
                setChecked("swap_send_and_back", Options.OPTION_SWAP_SEND_AND_BACK);
                // #sijapp cond.end#
                // #sijapp cond.if target is "MIDP2" | target is "MOTOROLA" #
                setChecked("auto_case",       Options.OPTION_TF_FLAGS);
                // #sijapp cond.end#
                setChecked("untitled_input",  Options.OPTION_UNTITLED_INPUT);
                
                // #sijapp cond.if modules_XSTATUSES is "true" #
                form.addString("status", null);
                setChecked("auto_xtraz",  Options.OPTION_AUTO_XTRAZ);
                // #sijapp cond.end #
                
                form.addString("messages", null);
                setChecked("delivery_notification", Options.OPTION_DELIVERY_NOTIFICATION);
                
                createSelector(
                        "fonts",
                        "fonts_smallest" + "|" + "fonts_small" + "|" + "fonts_normal" + "|" + "fonts_large" + "|" + "fonts_largest",
                        Options.OPTION_FONT_SCHEME);
                createSelector(
                        "message_send_mode",
                        "msm_normal" + "|" + "detransliterate" + "|" + "transliterate" + "|" + "manually",
                        Options.OPTION_MSGSEND_MODE);
                createSelector(
                        "input_mode",
                        "default" + "|" + "latin" + "|" + "cyrillic",
                        Options.OPTION_INPUT_MODE);
                loadOptionInt(Options.OPTION_MAX_MSG_COUNT, "max_message_count", 3);
                createSelector(
                        "keyboard_type",
                        "default" + "|" + "QWERTY" + "|" + "no",
                        Options.OPTION_KEYBOARD);
                break;
                
            case OPTIONS_SCHEME:
                showThemeSelector();
                return;
           case OPTIONS_SKIN:
                showSkinSelector();
                return;
				
            case OPTIONS_HOTKEYS:
                initHotkeyMenuUI();
                new Select(hotkey).show();
                return;
                
                /* Initialize elements (Signaling section) */
            case OPTIONS_SIGNALING:
                /* Vibrator notification controls */
                
                createSelector(
                        "vibration",
                        "no" + "|" + "yes" + "|" + "when_locked",
                        Options.OPTION_VIBRATOR);
                
                /* Message notification controls */
                // #sijapp cond.if modules_SOUND is "true" #
                createNotifyControls(Options.OPTION_MESS_NOTIF_MODE,
                        Options.OPTION_MESS_NOTIF_VOL,
                        Notify.NOTIFY_MESSAGE, "message_notification", false);
                // #sijapp cond.end#
                
                /* Online notification controls */
                // #sijapp cond.if modules_SOUND is "true" #
                createNotifyControls(Options.OPTION_ONLINE_NOTIF_MODE,
                        Options.OPTION_ONLINE_NOTIF_VOL,
                        Notify.NOTIFY_ONLINE, "onl_notification", false);
                // #sijapp cond.end#
                
                /* Typing notification controls */
                createSelector("typing_notify", 
                        "no" + "|" + "typing_incoming" + "|" + "typing_both",
                        Options.OPTION_TYPING_MODE);

                
                /* Popup windows control */
                createSelector("popup_win",
                        "no" + "|" + "pw_forme" + "|" + "pw_all",
                        Options.OPTION_POPUP_WIN2);
                
                
                setChecked("alarm", Options.OPTION_ALARM);
                setChecked("popup_win_over_system", Options.OPTION_POPUP_OVER_SYSTEM);
                // #sijapp cond.if target="MIDP2"#
                /* Midlet auto bring up controls on MIDP2 */
                setChecked("bring_up", Options.OPTION_BRING_UP);
                // #sijapp cond.end#
              
                setChecked("show_memory_alert", Options.OPTION_CUSTOM_GC);
                break;
                
                // #sijapp cond.if modules_ANTISPAM is "true"#
            case OPTIONS_ANTISPAM:
                setChecked("on", Options.OPTION_ANTISPAM_ENABLE);
                setChecked("cut_offline", Options.OPTION_ANTISPAM_OFFLINE);
                loadOptionString(Options.OPTION_ANTISPAM_MSG, "antispam_msg", 256, TextField.ANY);
                loadOptionString(Options.OPTION_ANTISPAM_ANSWER, "antispam_answer", 256, TextField.ANY);
                loadOptionString(Options.OPTION_ANTISPAM_HELLO, "antispam_hello", 256, TextField.ANY);
                loadOptionString(Options.OPTION_ANTISPAM_KEYWORDS, "antispam_keywords", 512, TextField.ANY);
                break;
                // #sijapp cond.end#
                
                // #sijapp cond.if modules_LIGHT is "true"#
            case OPTIONS_LIGHT:
                // "light_control"
                setChecked("on", Options.OPTION_LIGHT);
                
                loadLightValue("light_normal",    Options.OPTION_LIGHT_NONE);
                loadLightValue("light_key_press", Options.OPTION_LIGHT_KEY_PRESS);
                loadLightValue("light_message",   Options.OPTION_LIGHT_MESSAGE);
                loadLightValue("light_online",    Options.OPTION_LIGHT_ONLINE);
                loadLightValue("light_system",    Options.OPTION_LIGHT_SYSTEM);
                loadLightValue("light_normal_mode_timeout", Options.OPTION_LIGHT_TICK);
                break;
                // #sijapp cond.end#
                
                // #sijapp cond.if modules_ABSENCE is "true" #
            case OPTIONS_ABSENCE:
                setChecked("after_block", Options.OPTION_AA_BLOCK);
                form.addSelector(Options.OPTION_AA_TIME, "after_time", "off"+"|5 |10 |15 ", Options.getInt(Options.OPTION_AA_TIME) / 5);
                //form.addChoiceGroup(Options.OPTION_AUTOABSENCE, null, Choice.MULTIPLE);
                //setChecked(Options.OPTION_AUTOABSENCE, "autoanswer", Options.OPTION_AUTOABSENCE);
                //loadOptionString(Options.OPTION_AUTOANSWER, "answer", 256, TextField.ANY);
                break;
                // #sijapp cond.end#
				
                // #sijapp cond.if modules_ANSWERER is "true" #    
			case OPTIONS_ANSWERER:
				Answerer.getInstance().activate();
				return;
				// #sijapp cond.end#
				
                /* Initialize elements (cost section) */
                // #sijapp cond.if modules_TRAFFIC is "true"#
            case OPTIONS_TRAFFIC:
                loadOptionDecimal(Options.OPTION_COST_OF_1M, "cp1m");
                loadOptionDecimal(Options.OPTION_COST_PER_DAY, "cpd");
                form.addTextField(Options.OPTION_COST_PACKET_LENGTH,
                        "plength",
                        String.valueOf(Options.getInt(Options.OPTION_COST_PACKET_LENGTH) / 1024),
                        4, TextField.NUMERIC);
                loadOptionString(Options.OPTION_CURRENCY, "currency", 4, TextField.ANY);
                break;
                // #sijapp cond.end#
                
            case OPTIONS_TIMEZONE: {
                String items = "";
                int gmtOffset = Options.getInt(Options.OPTIONS_GMT_OFFSET);
                
                for (int i = -12; i <= 13; ++i) {
                    items += "|" + "GMT" + (i < 0 ? "" : "+") + i + ":00";
                }
                items = items.substring(1);
                form.addSelector(Options.OPTIONS_GMT_OFFSET, "time_zone", items, gmtOffset + 12);
                
                items = "";
                int cur = 0;

                int[] currDateTime = Util.createDate(Util.createCurrentDate(false));
                int minutes = currDateTime[Util.TIME_MINUTE];
                int hour = currDateTime[Util.TIME_HOUR];
                int startHour = hour - Options.getInt(Options.OPTIONS_LOCAL_OFFSET)
                        - Options.getInt(Options.OPTIONS_GMT_OFFSET) - 12;
                for (int i = startHour; i < (startHour + 24); ++i) {
                    items += "|" + ((i + 24) % 24) + (minutes < 10 ? ":0" : ":") + minutes;
                    if (hour == i) {
                        cur = i - startHour;
                    }
                }
                items = items.substring(1);
                form.addSelector(Options.OPTIONS_LOCAL_OFFSET, "local_time", items, cur);
                break;
            }
        }
        form.endForm();
        /* Activate options form */
        show();
    }
    private void show(CanvasEx back) {
        if (null != back) {
            form.show(back);
        } else {
            form.show();
        }
    }
    private void show() {
        form.show();
    }
    private void back() {
        form.back();
    }
}